#!/bin/bash

# 指定根目录
ROOT_DIR="/opt/1panel/apps/local/napcat"

# 白名单文件夹列表
WHITELIST=("nt_qq" "Crashpad" "crash_files")

# 存储所有非白名单文件夹的详细信息
all_non_whitelist_found=()

# 列出根目录下的文件夹
echo "在目录 $ROOT_DIR 查找到的文件夹："
folders=$(find "$ROOT_DIR" -mindepth 1 -maxdepth 1 -type d)

# 遍历每个文件夹
for folder in $folders; do
    folder_name=$(basename "$folder")

    # 构建字符框框的边界
    border="-------------------------------------------------"
    echo "+$border+"
    echo "| 目录名称: $folder_name/data/QQ"
    echo "|-------------------------------------------------|"

    # 检查子目录 /data/QQ 是否存在
    qq_dir="$folder/data/QQ"
    if [ -d "$qq_dir" ]; then
        # 初始化白名单和非白名单的显示内容
        whitelist_found=()
        non_whitelist_found=()

        # 查找 /data/QQ 下的所有文件夹
        subfolders=$(find "$qq_dir" -mindepth 1 -maxdepth 1 -type d)
        for subfolder in $subfolders; do
            subfolder_name=$(basename "$subfolder")

            # 判断是否在白名单中
            if [[ " ${WHITELIST[@]} " =~ (^|[[:space:]])"$subfolder_name"($|[[:space:]]) ]]; then
                whitelist_found+=("$subfolder_name")
            else
                subfolder_size=$(du -sh "$subfolder" 2>/dev/null | cut -f1)  # 获取文件夹大小
                if [ -n "$subfolder_size" ]; then
                    non_whitelist_found+=("$subfolder_name = $subfolder_size ")
                    all_non_whitelist_found+=("$subfolder")  # 只存储非白名单的文件夹路径
                else
                    non_whitelist_found+=("$subfolder_name (无法访问)")
                fi
            fi
        done

        # 输出白名单文件夹
        if [ ${#whitelist_found[@]} -gt 0 ]; then
            echo "| 白名单｛删掉需要重新登陆｝:" 
            for item in "${whitelist_found[@]}"; do
                echo "| - $item"
            done
            echo "|-------------------------------------------------|"
        fi

        # 输出非白名单的缓存文件夹
        if [ ${#non_whitelist_found[@]} -gt 0 ]; then
            echo "| 缓存文件夹｛可删｝:" 
            for item in "${non_whitelist_found[@]}"; do
                echo "| $item"
            done
        fi
    else
        echo "| $folder_name 下没有 /data/QQ 子目录"
    fi

    # 打印框的底部边界
    echo "+$border+"
done

# 询问用户是否要查看所有缓存文件夹的详细信息
echo "请输入选项："
echo "1 - 列出全部缓存文件夹"
echo "2 - 退出脚本"
read -p "请输入您的选择 (1 或 2): " user_choice

# 如果用户选择 "1"，则列出所有缓存文件夹详细信息
if [ "$user_choice" == "1" ] && [ ${#all_non_whitelist_found[@]} -gt 0 ]; then
    echo "全部缓存文件夹："
    for item in "${all_non_whitelist_found[@]}"; do
        # 直接输出路径，不显示大小
        echo "| $item"
    done

    # 提供新的选择：清空缓存或退出脚本
    echo "请输入选项："
    echo "1 - 清空缓存"
    echo "2 - 退出脚本"
    read -p "请输入您的选择 (1 或 2): " action_choice

    if [ "$action_choice" == "1" ]; then
        # 用户选择清空缓存，进一步确认是否删除
        echo "确认删除以下缓存文件夹："
        for item in "${all_non_whitelist_found[@]}"; do
            echo "| $item"
        done

        read -p "确认删除这些缓存文件夹吗？ (y/Y 确定删除, n/N 取消删除): " confirm_delete

        if [[ "$confirm_delete" == "y" || "$confirm_delete" == "Y" ]]; then
            echo "正在删除缓存文件夹..."

            # 删除所有非白名单文件夹
            for item in "${all_non_whitelist_found[@]}"; do
                # 删除非白名单文件夹，并确认路径正确
                if [ -d "$item" ]; then
                    echo "删除文件夹: $item"
                    rm -rf "$item"
                else
                    echo "文件夹不存在，跳过: $item"
                fi
            done

            echo "缓存文件夹删除完毕。"
        elif [[ "$confirm_delete" == "n" || "$confirm_delete" == "N" ]]; then
            echo "取消删除操作，退出脚本。"
        else
            echo "无效输入，退出脚本。"
        fi
    elif [ "$action_choice" == "2" ]; then
        # 用户选择退出
        echo "退出脚本。"
    else
        # 无效输入
        echo "无效输入，退出脚本。"
    fi
elif [ "$user_choice" == "2" ]; then
    # 用户选择退出
    echo "退出脚本。"
else
    echo "无效输入，退出脚本。"
fi
